Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manifest: support NEP-24 #3560

Merged
merged 4 commits into from
Nov 20, 2024
Merged

manifest: support NEP-24 #3560

merged 4 commits into from
Nov 20, 2024

Conversation

AliceInHunterland
Copy link
Contributor

@AliceInHunterland AliceInHunterland commented Aug 19, 2024

Close #3451

wrapper generates:

// RoyaltyInfo invokes `royaltyInfo` method of contract.
func (c *ContractReader) RoyaltyInfo(tokenID []byte, royaltyToken util.Uint160, salePrice *big.Int) ([]stackitem.Item, error) {
	return unwrap.Array(c.invoker.Call(c.hash, "royaltyInfo", tokenID, royaltyToken, salePrice))
}

but it should be different.

add tests with smartcontact/testdata/

Copy link

codecov bot commented Sep 9, 2024

Codecov Report

Attention: Patch coverage is 71.91011% with 50 lines in your changes missing coverage. Please review.

Project coverage is 82.97%. Comparing base (57eec71) to head (b63c7aa).
Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
pkg/rpcclient/nep24/royalty.go 65.45% 25 Missing and 13 partials ⚠️
pkg/smartcontract/rpcbinding/binding.go 85.93% 7 Missing and 2 partials ⚠️
pkg/smartcontract/manifest/standard/comply.go 25.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3560      +/-   ##
==========================================
- Coverage   83.02%   82.97%   -0.06%     
==========================================
  Files         334      335       +1     
  Lines       46543    46708     +165     
==========================================
+ Hits        38643    38755     +112     
- Misses       6326     6363      +37     
- Partials     1574     1590      +16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

pkg/rpcclient/nep11/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep11/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep11/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep11/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep11/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep11/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep11/royalty_test.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep11/royalty.go Outdated Show resolved Hide resolved
pkg/smartcontract/manifest/standard/nep24.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty.go Show resolved Hide resolved
pkg/rpcclient/nep24/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty_test.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty_test.go Show resolved Hide resolved
pkg/smartcontract/manifest/standard/nep24.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you need to adjust

func comply(m *manifest.Manifest, checkNames bool, st *Standard) error {
in the following way:

  1. Extend standard.Standard structure with Required []string field. This field includes standards required by this standard.
  2. In comply() check that standards marked as "Required" are present in the manifest. No compliance check is needed for them, the presence check is enough since compliance is checked by the calling code for all standards.

Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the test of RPC bindings generator based on the existing NEP contract is missing, we discussed it in DM.

Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review is not finished, some new code is uploaded.

examples/nft-nd/nft.go Outdated Show resolved Hide resolved
Comment on lines 16 to 27
- name: RoyaltiesTransferred
parameters:
- name: royaltyToken
type: Hash160
- name: royaltyRecipient
type: Hash160
- name: buyer
type: Hash160
- name: tokenId
type: ByteArray
- name: amount
type: Integer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for the NEP11 contract itself, read the spec:

Marketplaces that support this standard MUST emit the event, RoyaltiesTransferred for each recipient, after sending a payment.

Copy link
Contributor Author

@AliceInHunterland AliceInHunterland Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I should remove it from manifest/standard too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's not a part of the standard that belongs to the NEP11 contract itself. But it should be moved to a separate standard, ref. #3560 (comment).

examples/nft-nd/nft.yml Outdated Show resolved Hide resolved
Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is not finished. I need a test with generated RPC bindings.

pkg/rpcclient/nep24/royalty.go Outdated Show resolved Hide resolved
examples/nft-nd/nft.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty.go Outdated Show resolved Hide resolved
Comment on lines 64 to 80
var royalties []RoyaltyRecipient
for _, item := range res.Stack {
royalty, ok := item.Value().([]stackitem.Item)
if !ok || len(royalty) != 2 {
return nil, fmt.Errorf("invalid royalty structure: expected array of 2 items, got %d", len(royalty))
}
var recipient RoyaltyRecipient
err = recipient.FromStackItem(royalty)
if err != nil {
return nil, fmt.Errorf("failed to decode royalty detail: %w", err)
}
royalties = append(royalties, recipient)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting for the response.

pkg/smartcontract/manifest/standard/comply.go Show resolved Hide resolved
pkg/smartcontract/manifest/standard/nep24.go Show resolved Hide resolved
pkg/smartcontract/manifest/standard/comply.go Outdated Show resolved Hide resolved
examples/nft-nd/nft.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Show resolved Hide resolved
@AnnaShaleva
Copy link
Member

AnnaShaleva commented Oct 25, 2024

wrapper generates:

// RoyaltyInfo invokes royaltyInfo method of contract.
func (c *ContractReader) RoyaltyInfo(tokenID []byte, royaltyToken util.Uint160, salePrice *big.Int) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.Call(c.hash, "royaltyInfo", tokenID, royaltyToken, salePrice))
}

but it should be different.

What is your expected result? To me, the auto-generated code looks good although it does not includes named return structures, but it depends on the original contract. As I said earlier, it's a matter of separate issue anyway.

@AliceInHunterland
Copy link
Contributor Author

i have:

// RoyaltyInfo invokes `royaltyInfo` method of contract.
func (c *RoyaltyReader) RoyaltyInfo(tokenID []byte, royaltyToken util.Uint160, salePrice *big.Int) ([]stackitem.Item, error) {
	return unwrap.Array(c.Invoker.Call(c.Hash, "royaltyInfo", tokenID, royaltyToken, salePrice))
}

and:

func TestRoyaltyReaderRoyaltyInfo2(t *testing.T) {
	ta := new(testAct)
	rr := &RoyaltyReader{
		Invoker: ta,
		Hash:    util.Uint160{1, 2, 3},
	}

	tokenID := []byte{1, 2, 3}
	royaltyToken := util.Uint160{4, 5, 6}
	salePrice := big.NewInt(1000)

	tests := []struct {
		name       string
		setupFunc  func()
		expectErr  bool
		expectedRI []RoyaltyRecipient
	}{
		{
			name: "error case",
			setupFunc: func() {
				ta.err = errors.New("some error")
			},
			expectErr: true,
		},
		{
			name: "valid response",
			setupFunc: func() {
				ta.err = nil
				recipient := util.Uint160{7, 8, 9}
				amount := big.NewInt(100)
				ta.res = &result.Invoke{
					State: "HALT",
					Stack: []stackitem.Item{
						stackitem.Make([]stackitem.Item{
							stackitem.Make(recipient.BytesBE()),
							stackitem.Make(amount),
						}),
					},
				}
			},
			expectErr: false,
			expectedRI: []RoyaltyRecipient{
				{
					Address: util.Uint160{7, 8, 9},
					Amount:  big.NewInt(100),
				},
			},
		},
		{
			name: "invalid data response",
			setupFunc: func() {
				ta.res = &result.Invoke{
					State: "HALT",
					Stack: []stackitem.Item{
						stackitem.Make([]stackitem.Item{
							stackitem.Make(util.Uint160{7, 8, 9}.BytesBE()),
						}),
					},
				}
			},
			expectErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.setupFunc()
			riStackItems, err := rr.RoyaltyInfo(tokenID, royaltyToken, salePrice)
			if tt.expectErr {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
				require.Len(t, riStackItems, len(tt.expectedRI))

				var actualRI []RoyaltyRecipient
				for _, item := range riStackItems {
					subItems, ok := item.Value().([]stackitem.Item)
					require.True(t, ok)
					require.Len(t, subItems, 2)

					recipientBytes, err := subItems[0].TryBytes()
					require.NoError(t, err)
					recipient, err := util.Uint160DecodeBytesBE(recipientBytes)
					require.NoError(t, err)

					amount, err := subItems[1].TryInteger()
					require.NoError(t, err)

					actualRI = append(actualRI, RoyaltyRecipient{
						Address: recipient,
						Amount:  amount,
					})
				}

				require.Equal(t, tt.expectedRI, actualRI)
			}
		})
	}
}

and:


=== RUN   TestRoyaltyReaderRoyaltyInfo2/valid_response
    royalty_test.go:183: 
        	Error Trace:	/Users/ekaterinapavlova/Workplace/neo-go/pkg/rpcclient/nep24/royalty_test.go:183
        	Error:      	"[ByteString BigInteger]" should have 1 item(s), but has 2
        	Test:       	TestRoyaltyReaderRoyaltyInfo2/valid_response
--- FAIL: TestRoyaltyReaderRoyaltyInfo2/valid_response (0.00s)

=== RUN   TestRoyaltyReaderRoyaltyInfo2/invalid_data_response
    royalty_test.go:180: 
        	Error Trace:	/Users/ekaterinapavlova/Workplace/neo-go/pkg/rpcclient/nep24/royalty_test.go:180
        	Error:      	An error is expected but got nil.
        	Test:       	TestRoyaltyReaderRoyaltyInfo2/invalid_data_response
--- FAIL: TestRoyaltyReaderRoyaltyInfo2/invalid_data_response (0.00s)

@AliceInHunterland
Copy link
Contributor Author

wrapper generates:
// RoyaltyInfo invokes royaltyInfo method of contract.
func (c *ContractReader) RoyaltyInfo(tokenID []byte, royaltyToken util.Uint160, salePrice *big.Int) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.Call(c.hash, "royaltyInfo", tokenID, royaltyToken, salePrice))
}
but it should be different.

What is your expected result? To me, the auto-generated code looks good although it does not includes named return structures, but it depends on the original contract. As I said earlier, it's a matter of separate issue anyway.

I wrongly passed as a config file to generate-rpcbinding the same config file as used for contract compile which is why I expected a different result (something like:

func (c *ContractReader) RoyaltyInfo(tokenID []byte, royaltyToken util.Uint160, salePrice *big.Int) ([]*TestnepRoyaltyRecipient, error) {
). Also tried to write the test for it #3560 (comment) and with that implementation, it didn't work.

@roman-khimov
Copy link
Member

I wrongly passed as a config file to generate-rpcbinding the same config file as used for contract compile

You're not first one to do this, btw. @smallhive and @tatiana-nspcc both know it happens easily. Long-term this will be solved by removing binding configuration file because of NEP-25.

Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3560 (comment) is still missing.

cli/smartcontract/generate_test.go Show resolved Hide resolved
cli/smartcontract/generate_test.go Outdated Show resolved Hide resolved
examples/nft-d/nft.go Outdated Show resolved Hide resolved
examples/nft-d/nft.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty_test.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty_test.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty_test.go Outdated Show resolved Hide resolved
pkg/smartcontract/manifest/standard/comply.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
examples/nft-d/nft.go Outdated Show resolved Hide resolved
examples/nft-d/nft.go Outdated Show resolved Hide resolved
examples/nft-d/nft.yml Outdated Show resolved Hide resolved
examples/nft-d/nft.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
examples/nft-nd/nft.yml Outdated Show resolved Hide resolved
Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

cli/smartcontract/generate_test.go Show resolved Hide resolved
pkg/rpcclient/nep24/royalty.go Outdated Show resolved Hide resolved
pkg/smartcontract/manifest/manifest.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
examples/nft-nd/nft.go Outdated Show resolved Hide resolved
examples/nft-nd/nft.go Outdated Show resolved Hide resolved
examples/nft-nd/nft.go Outdated Show resolved Hide resolved
Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job, some minor changes are required, otherwise LGTM.

examples/nft-d/nft.go Outdated Show resolved Hide resolved
examples/nft-d/nft.yml Show resolved Hide resolved
examples/nft-nd/nft.go Outdated Show resolved Hide resolved
pkg/rpcclient/nep24/royalty.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
pkg/smartcontract/rpcbinding/binding.go Outdated Show resolved Hide resolved
examples/README.md Outdated Show resolved Hide resolved
pkg/smartcontract/manifest/standard/nep24.go Outdated Show resolved Hide resolved
`Required` contains standards that are required for this standard.

Signed-off-by: Ekaterina Pavlova <[email protected]>
Close #3451

Signed-off-by: Ekaterina Pavlova <[email protected]>
Signed-off-by: Ekaterina Pavlova <[email protected]>
@AnnaShaleva AnnaShaleva merged commit 2b758c5 into master Nov 20, 2024
32 of 34 checks passed
@AnnaShaleva AnnaShaleva deleted the nep24 branch November 20, 2024 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add NEP-24 to known manifests, check compliance
3 participants